package cn.gov.pbc.gd.credit.core.service.inquiry.impl;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.ResultVo;
import com.liu.jbase.service.impl.BaseServiceImpl;
import com.liu.tools.LogUtil;
import com.liu.tools.StrUtil;

import cn.gov.pbc.gd.credit.api.inquiry.IInquiryApi;
import cn.gov.pbc.gd.credit.center.service.charge.ICCChargeService;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.ConfConstant;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.core.service.InquiryUtil;
import cn.gov.pbc.gd.credit.core.service.inquiry.IChargeService;
import cn.gov.pbc.gd.credit.user.service.realuser.IRealUserService;
import cn.gov.pbc.gd.credit.vo.center.ResCCUserVo;
import cn.gov.pbc.gd.credit.vo.center.charge.ReqCCChargeVo;
import cn.gov.pbc.gd.credit.vo.center.charge.ResCCChargeVo;
import cn.gov.pbc.gd.credit.vo.inquiry.InquiryVo;
import cn.gov.pbc.gd.credit.vo.inquiry.ResInquiryVo;

@Service
public class ChargeServiceImpl extends BaseServiceImpl<ResInquiryVo, InquiryVo, IInquiryApi> implements IChargeService {

	private static Logger logger = LoggerFactory.getLogger(ChargeServiceImpl.class);
	
	@Autowired
	private ICCChargeService ccChargeService;
	
	@Autowired
	private IInquiryApi inquiryApi;
	
	@Autowired
	private IRealUserService realUserService;
	
	@Autowired
	private IRedisService redisService;
	
	@Override
	public ResultVo<ResCCChargeVo> charge(ReqCCChargeVo chargeVo, HttpServletRequest request) {

		return chargeHelper(chargeVo, request, 1);
	}

	@Override
	public ResultVo<ResCCChargeVo> cancelCharge(ReqCCChargeVo chargeVo, HttpServletRequest request) {

		return chargeHelper(chargeVo, request, 0);
	}

	private ResultVo<ResCCChargeVo> chargeHelper(ReqCCChargeVo chargeVo, HttpServletRequest request, Integer isCharge){
		
        ResultVo<ResCCChargeVo> resultVo = new ResultVo<>();
		
		ResCCUserVo resCCUserVo = InquiryUtil.getResCCUserVo(request, redisService, realUserService);
		
		if(StrUtil.isNull(ConfConstant.ccTestPath) && resCCUserVo == null){
			
			logger.error(LogUtil.msg("InquiryServiceImpl:chargeHelper", "登录征信中心失败", JSON.toJSONString(chargeVo)));
			resultVo.setCode(CodeConstant.FAIL);
			resultVo.setMsg("查询失败（不能登录征信中心）");
			return resultVo;		
		}
		
		if(resCCUserVo != null){
			chargeVo.setCookies(resCCUserVo.getCookies());
		}
		if(StrUtil.isNotNull(chargeVo.getCtype())){
			chargeVo.setCtype(InquiryUtil.getCardType(Integer.parseInt(chargeVo.getCtype())));
		}
		
		if(isCharge == 1){
			logger.info(LogUtil.msg("InquiryServiceImpl:chargeHelper", "收费业务：", JSON.toJSONString(chargeVo)));
			resultVo = ccChargeService.charge(chargeVo, request);
		} else {
			logger.info(LogUtil.msg("InquiryServiceImpl:chargeHelper", "取消收费业务：", JSON.toJSONString(chargeVo)));
			resultVo = ccChargeService.cancelCharge(chargeVo, request);
		}
		
		if(CodeConstant.SUCCESS.equals(resultVo.getCode())){
			
			InquiryVo inquiryVo = new InquiryVo();
			inquiryVo.setId(chargeVo.getId());
			if(isCharge == 1){
				logger.info(LogUtil.msg("InquiryServiceImpl:chargeHelper", "征信中心收费成功：", JSON.toJSONString(resultVo)));
				inquiryVo.setChargeNo(resultVo.getData().getChargeNo());
				inquiryVo.setFeeAmount(10.00);
				inquiryVo.setChargeResult(1);
			} else {
				logger.info(LogUtil.msg("InquiryServiceImpl:chargeHelper", "征信中心取消成功：", JSON.toJSONString(resultVo)));
				inquiryVo.setFeeAmount(0.0);//取消缴费
				inquiryVo.setChargeResult(2);
			}
			
			try {
				inquiryApi.update(inquiryVo);
			} catch (Exception e) {
				logger.error(LogUtil.msg("ChargeServiceImpl:chargeHelper", "修改查询缴费信息异常"), e);
			}
			
		} else {
			logger.info(LogUtil.msg("InquiryServiceImpl:chargeHelper", "征信中心收费或取消收费失败：", JSON.toJSONString(resultVo)));
		}
		
		return resultVo;
		
	}

}
